﻿using System;
using System.IO;
using Ini;

namespace SSFT
{
    class Settings
    {
        const string DefaultClientHostname = "127.0.0.1";
        const int DefaultClientPort = 43000;
        const int DefaultServerPort = 43000;
        const int DefaultServerMaxConns = 8;

        string programPath;
        string iniSettingsPath;
        IniFile iniSettingsFile;

        public Settings()
        {
            programPath = Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ".ssft");

            if (!Directory.Exists(programPath))
                Directory.CreateDirectory(programPath);
            
            iniSettingsPath = Path.Combine(programPath, "settings.ini");
            iniSettingsFile = new IniFile(iniSettingsPath);
            if (!File.Exists(iniSettingsPath))
            {
                iniSettingsFile.IniWriteValue("Client", "hostname", DefaultClientHostname);
                iniSettingsFile.IniWriteValue("Client", "port", DefaultClientPort.ToString());
                iniSettingsFile.IniWriteValue("Server", "port", DefaultServerPort.ToString());
                iniSettingsFile.IniWriteValue("Server", "maxconns", DefaultServerMaxConns.ToString());
            }
        }

        public string Get(string section, string key)
        {
            return iniSettingsFile.IniReadValue(section, key, "");
        }

        public void Set(string section, string key, string value)
        {
            iniSettingsFile.IniWriteValue(section, key, value);
        }
    }
}
